package com.aixuexi.item.service.impl;

import com.aixuexi.item.entity.Category;
import com.aixuexi.item.dao.CategoryDao;
import com.aixuexi.item.service.CategoryService;
import com.aixuexi.item.vo.CategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2022-02-14 23:28:17
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Category queryById(Integer id) {
        return this.categoryDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param category 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Category> queryByPage(Category category, PageRequest pageRequest) {
        long total = this.categoryDao.count(category);
        return new PageImpl<>(this.categoryDao.queryAllByLimit(category, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param category 实例对象
     * @return 实例对象
     */
    @Override
    public Category insert(Category category) {
        this.categoryDao.insert(category);
        return category;
    }

    /**
     * 修改数据
     *
     * @param category 实例对象
     * @return 实例对象
     */
    @Override
    public Category update(Category category) {
        this.categoryDao.update(category);
        return this.queryById(category.getId());
    }

    /**
     * 通过主键删除数据
     *
     *
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String ids) {
        if (StringUtils.isNotBlank(ids)){
            String[] split = ids.split(",");
            for (String s : split) {
                categoryDao.deleteById(Integer.parseInt(s));
            }
        }
        return true;
    }


    @Override
    public List<CategoryVo> findAll() {
        return categoryDao.findAll();
    }

    @Override
    public List<CategoryVo> getCategoriesByName(String categoryName) {
        return categoryDao.getCategoriesByName(categoryName);
    }
}
